package com.maserb.monitor.web.common;

import com.maserb.monitor.common.AjaxResult;
import com.maserb.monitor.common.AjaxUtils;
import com.maserb.monitor.common.Func_T;
import com.maserb.monitor.service.common.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/file")
@Slf4j
public class FileController {
    @Autowired
    FileService fileService;

    @RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
    public AjaxResult<String[]> upload(@RequestParam(name = "files[]") MultipartFile[] files) {
        return AjaxUtils.process(new Func_T<String[]>() {
            @Override
            public String[] invoke() throws Exception {
                String[] result = new String[files.length];

                try {
                    for (int i = 0; i < files.length; i++) {
                        if (files[i] != null) {
                            result[i] = fileService.upload(files[i]);
                        }
                    }
                } catch (Exception e) {
                    log.error("error to upload files", e);
                }

                return result;
            }
        });
    }

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult<String> uploadFile(@RequestParam(name = "file") MultipartFile file) {
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                return fileService.upload(file);
            }
        });
    }

    @RequestMapping(value = "/uploadFile_head", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult<String> uploadFile_head(@RequestParam(name = "file") MultipartFile file,String schoolId,String studentId) {
        return AjaxUtils.process(new Func_T<String>() {
            @Override
            public String invoke() throws Exception {
                return fileService.uploadHead(file,schoolId,studentId);
            }
        });
    }


    @RequestMapping("/downloadImage")
    public ResponseEntity<byte[]> downloadImage(@RequestParam("filename") String filename) throws Exception {
        return fileService.downloadImage(filename);
    }

    @RequestMapping("/downloadFile")
    public ResponseEntity<byte[]> downloadFile(@RequestParam("filename") String filename, HttpResponse response) throws Exception {
        return fileService.downloadFile(filename);
    }

    @RequestMapping("/downloadAudio")
    public void downloadAudio(HttpServletResponse response, @RequestParam("filename") String filename ) throws Exception {
        fileService.downloadAudio(filename,response);
    }

    @RequestMapping("/downloadFileByUrl")
    public void downloadFileByUrl(HttpServletRequest request, HttpServletResponse response,@RequestParam("filename") String filename ) throws Exception {
        fileService.downloadFileByUrl(request,response,filename);
    }


    @RequestMapping("/testUpload")
    public String testUpload() {
        return "common/test_upload";
    }
}
